<?php


namespace app\admin\controller;


use think\Exception;

class SpecsValue
{
    public function getBySpecsId()
    {
        $specs_id = input('specs_id', '', 'intval');

        if (!$specs_id) {
            return show(1, 'ok');
        }

        $res = (new \app\common\business\Specs())->getBySpecsId($specs_id);
        return show(1, 'ok', $res);
    }

    public function save()
    {
        $specs_id = input('specs_id', '', 'intval');
        $name = input('name', '', 'trim');

        $data = [
            'specs_id' => $specs_id,
            'name' => $name,
        ];


        try {
            validate(\app\admin\validate\Specs::class)->scene('add')->check($data);
        } catch (Exception $exception) {
            return show(2, 'error');
        }

        $id = (new \app\common\business\Specs())->add($data);

        if (!$id) {
            return show(2, 'error');
        }

        return show(1, 'ok', ['id' => $id]);
    }

    public function del()
    {
        $specs_id = input('specs_id', '', 'intval');

        $res = (new \app\common\business\Specs())->del($specs_id);

        if (!$res) {
            return show(2, 'error');
        }

        return show(1, 'ok');

    }

}